import { Service } from 'egg';

class ConclusionService extends Service {
  /** 查找各个conclusion对应的questionList */
  async searchQuestionListByConclusion() {
    const { ctx } = this;
    const groupConclusionQuestion = await ctx.model.Conclusion.findAll({
      include: [
        {
          model: ctx.model.Question,
          attributes: [
            ['id', 'questionId'],
          ],
          through: { attributes: [] },
        }
      ],
      attributes: [
        ['id', 'conclusionId'],
        ['name', 'conclusionName']
      ],
      order: [['id', 'ASC']],
    });

    const conclusionList = groupConclusionQuestion.map((conclusionItem) => {
      const { conclusionId, conclusionName, Questions } = conclusionItem.dataValues;
      const questionList = Questions.map((questionItem) => questionItem.dataValues.questionId);
      return {
        conclusionId,
        conclusionName,
        questionList
      };
    });

    return conclusionList;
  }
}

module.exports = ConclusionService;
